programming4us
           
 
 
Windows

Scripting Windows 7 with WSH : Programming the WshShell Object (part 1)

- Free product key for windows 10
- Free Product Key for Microsoft office 365
- Malwarebytes Premium 3.7.1 Serial Keys (LifeTime) 2019
5/2/2011 5:43:32 PM

WshShell is a generic name for a powerful object that enables you to query and interact with various aspects of the Windows shell. You can display information to the user, run applications, create shortcuts, work with the Registry, and control Windows environment variables. The next few sections discuss each of those useful tasks.

Referencing the WshShell Object

WshShell refers to the Shell object exposed via the automation interface of WScript. Therefore, you must use CreateObject to return this object:

Set objWshShell = WScript.CreateObject("WScript.Shell")

From here, you can use the objWshShell variable to access the object’s properties and methods.

Displaying Information to the User

You saw earlier that the WScript object’s Echo method is useful for displaying simple text messages to the user. You can gain more control over the displayed message by using the WshShell object’s Popup method. This method is similar to the MsgBox function used in Visual Basic and VBA in that it enables you to control both the dialog box title and the buttons displayed, and to determine which of those buttons the user pressed. Here’s the syntax:

WshShell.Popup(strText [, nSecondsToWait] [, strTitle] [, intType])

WshShellThe WshShell object.
strTextThe message you want to display in the dialog box. You can enter a string up to 1,024 characters long.
nSecondsToWaitThe maximum number of seconds the dialog box will be displayed.
strTitleThe text that appears in the dialog box title bar. If you omit this value, Windows Script Host appears in the title bar.
intTypeA number or constant that specifies, among other things, the command buttons that appear in the dialog box (see the next section). The default value is 0.

For example, the following statements display the dialog box shown in Figure 1:

Set objWshShell = WScript.CreateObject("WScript.Shell")
objWshShell.Popup "Couldn't find Memo.doc!", , "Warning"

Figure 1. A simple message dialog box produced by the Popup method.


Tip

For long messages, VBScript wraps the text inside the dialog box. If you prefer to create your own line breaks, use VBScript’s Chr function and the carriage return character (ASCII 13) between each line:

WshShell.Popup "First line" & Chr(13) & "Second line"

For JavaScript, use \n instead:

WshShell.Popup("First line" + "\n" + "Second line");


Setting the Style of the Message

The default Popup dialog box displays only an OK button. You can include other buttons and icons in the dialog box by using different values for the intType parameter. Table 1 lists the available options.

Table 1. Popup Method’s intType Parameter Options
VBScript ConstantValueDescription
Buttons  
vbOKOnly0Displays only an OK button. This is the default.
vbOKCancel1Displays the OK and Cancel buttons.
vbAbortRetryIgnore2Displays the Abort, Retry, and Ignore buttons.
vbYesNoCancel3Displays the Yes, No, and Cancel buttons.
vbYesNo4Displays the Yes and No buttons.
vbRetryCancel5Displays the Retry and Cancel buttons.
Icons  
vbCritical16Displays the Critical Message icon.
vbQuestion32Displays the Warning Query icon.
vbExclamation48Displays the Warning Message icon.
vbInformation64Displays the Information Message icon.
Default Buttons  
vbDefaultButton10The first button is the default (that is, the button selected when the user presses Enter).
vbDefaultButton2256The second button is the default.
vbDefaultButton3512The third button is the default.

You derive the intType argument in one of two ways:

  • By adding the values for each option

  • By using the VBScript constants separated by plus signs (+)

The script in Listing 1 shows an example and Figure 2 shows the resulting dialog box.

Figure 2. Right-clicking a VBScript file reveals the new Run as Administrator command.


Listing 1. VBScript Example That Uses the Popup Method to Display the Dialog Box
' First, set up the message
'
strText = "Are you sure you want to copy" & Chr(13)
strText = strText & "the selected files to drive G?"
strTitle = "Copy Files"
intType = vbYesNoCancel + vbQuestion + vbDefaultButton2
'
' Now display it
'
Set objWshShell = WScript.CreateObject("WScript.Shell")
intResult = objWshShell.Popup(strText, ,strTitle, intType)

Here, three variables—strText, strTitle, and intType—store the values for the Popup method’s strText, strTitle, and intType arguments, respectively. In particular, the following statement derives the intType argument:

intType = vbYesNoCancel + vbQuestion + vbDefaultButton2

You also could derive the intType argument by adding up the values that these constants represent (3, 32, and 256, respectively), but the script becomes less readable that way.

Getting Return Values from the Message Dialog Box

A dialog box that displays only an OK button is straightforward. The user either clicks OK or presses Enter to remove the dialog from the screen. The multibutton styles are a little different, however; the user has a choice of buttons to select, and your script should have a way to find out which button the user chose, which enables it to decide what to do next, based on the user’s selection. You do this by storing the Popup method’s return value in a variable. Table 2 lists the seven possible return values.

Table 2. Popup Method’s Return Values
VBScript ConstantValueButton Selected
vbOK1OK
vbCancel2Cancel
vbAbort3Abort
vbRetry4Retry
vbIgnore5Ignore
vbYes6Yes
vbNo7No

To process the return value, you can use an If...Then...Else or Select Case structure to test for the appropriate values. For example, the script shown earlier used a variable called intResult to store the return value of the Popup method. Listing 30.5 shows a revised version of the script that uses a VBScript Select Case statement to test for the three possible return values.

Listing 2. Script That Uses a Select Case Statement to Process the Popup Method’s Return Value
' First, set up the message
'
strText = "Are you sure you want to copy" & Chr(13)
strText = strText & "the selected files to drive A?"
strTitle = "Copy Files"
intType = vbYesNoCancel + vbQuestion + vbDefaultButton2
'
' Now display it
'
Set objWshShell = WScript.CreateObject("WScript.Shell")
intResult = objWshShell.Popup(strText, ,strTitle, intType)
'
' Process the result
'
Select Case intResult
Case vbYes
WScript.Echo "You clicked ""Yes""!"
Case vbNo
WScript.Echo "You clicked ""No""!"
Case vbCancel
WScript.Echo "You clicked ""Cancel""!"
End Select


Running Applications

When you need your script to launch another application, use the Run method:

WshShell.Run strCommand [, intWindowStyle] [, bWaitOnReturn]

WshShellThe WshShell object.
strCommandThe name of the file that starts the application. Unless the file is in the Windows folder, you should include the drive and folder to make sure that the script can find the file.
intWindowStyleA constant or number that specifies how the application window will appear:
 intWindowStyleWindow Appearance
 0Hidden
 1Normal size with focus
 2Minimized with focus (this is the default)
 3Maximized with focus
 4Normal without focus
 6Minimized without focus
bWaitOnReturnA logical value that determines whether the application runs asynchronously. If this value is True, the script halts execution until the user exits the launched application. If this value is False, the script continues running after it has launched the application.

Here’s an example:

Set objWshShell = WScript.CreateObject("WScript.Shell")
objWshShell.Run "Control.exe Inetcpl.cpl", 1, True

This Run method launches Control Panel’s Internet Properties dialog box.

Other -----------------
- Scripting Windows 7 with WSH : Programming the WScript Object
- Scripting Windows 7 with WSH : Programming Objects
- Scripting Windows 7 with WSH : Scripts and Script Execution
- Adding Macs to Your Windows 7 Network : Letting Windows Computers See Your Mac Shares
- Adding Macs to Your Windows 7 Network : Using a Mac to Make a Remote Desktop Connection to Windows 7
- Adding Macs to Your Windows 7 Network : Connecting to a Windows Shared Folder
- Adding Macs to Your Windows 7 Network : Connecting to the Windows Network
- Windows 7 : Controlling and Customizing Your Website (part 5) - Viewing the Server Logs
- Windows 7 : Controlling and Customizing Your Website (part 4) - Disabling Anonymous Access
- Windows 7 : Controlling and Customizing Your Website (part 3) - Working Without a Default Document
- Windows 7 : Controlling and Customizing Your Website (part 2) - Setting the Website’s Default Document
- Windows 7 : Controlling and Customizing Your Website (part 1)
- Windows 7 : Adding Folders and Files to the Default Website (part 3) - Adding a Folder to the Default Website
- Windows 7 : Adding Folders and Files to the Default Website (part 2) - Changing the Default Website Home Page
- Windows 7 : Adding Folders and Files to the Default Website (part 1) - Setting Permissions on the Default Website Folder
- Turning Windows 7 into a Web Server : Understanding the Default Website
- Turning Windows 7 into a Web Server : Accessing Your Website
- Windows 7 : Installing Internet Information Services
- Windows 7 : Using Virtual Private Network Connections
- Windows 7 : Using Dynamic DNS to Access Your Network & Configuring a Network Computer for Remote Administration
 
 
 
Top 10
 
- Microsoft Visio 2013 : Adding Structure to Your Diagrams - Finding containers and lists in Visio (part 2) - Wireframes,Legends
- Microsoft Visio 2013 : Adding Structure to Your Diagrams - Finding containers and lists in Visio (part 1) - Swimlanes
- Microsoft Visio 2013 : Adding Structure to Your Diagrams - Formatting and sizing lists
- Microsoft Visio 2013 : Adding Structure to Your Diagrams - Adding shapes to lists
- Microsoft Visio 2013 : Adding Structure to Your Diagrams - Sizing containers
- Microsoft Access 2010 : Control Properties and Why to Use Them (part 3) - The Other Properties of a Control
- Microsoft Access 2010 : Control Properties and Why to Use Them (part 2) - The Data Properties of a Control
- Microsoft Access 2010 : Control Properties and Why to Use Them (part 1) - The Format Properties of a Control
- Microsoft Access 2010 : Form Properties and Why Should You Use Them - Working with the Properties Window
- Microsoft Visio 2013 : Using the Organization Chart Wizard with new data
- First look: Apple Watch

- 3 Tips for Maintaining Your Cell Phone Battery (part 1)

- 3 Tips for Maintaining Your Cell Phone Battery (part 2)
programming4us programming4us